<?php

namespace app\index\controller;

use app\index\model\Jymd;
use think\Db;
use think\Model;

class Xmb extends Base
{
    public  function index(){

        return $this->fetch();
    }
    /**
     * @return mixed
     * 项目复制
     */

    public  function xmcopy(){
        $res = Model('xmb')->GetXmbDh("AC40%");
        foreach ($res as $k =>$va['xmdh']){
            //获取当前需要添加的最大项目代号
            $endXmdh = substr($va['xmdh']['xmdh'],2) + 1;
            $data['endXmdh'] = 'AC'.$endXmdh;
            //获取最大的项目编号
            $endXmbh = substr($va['xmdh']['xmbh'],1) + 1;
            $data['endXmbh']  = 'Y'.$endXmbh;
            break;
            //终止循环
        }
        //获取最大的检验目的
        $jymdRes = Model('jymd')->GetJymdDhDesc("ac3000%");
        foreach ($jymdRes as $k =>$va['jymddh']) {
            //获取最大的检验目的最大代号
            $endJymddh = substr($va['jymddh']['jymddh'],2) + 1;
            $data['endJymddh'] = 'AC'.$endJymddh;
            break;
        }

        $this->assign('data',$data);
        return $this->fetch('xmb/xmcopy');
    }

    /*
     *  项目复制 提交
     * */
    public function xmcopybyxmdh()
    {

        $xmdh = $_POST['xmdh'];
        $newxmmc = $_POST['newxmmc'];
        $newxmdh = $_POST['newxmdh'];
        $newxmbh = $_POST['newxmbh'];
        if (!$xmdh || !$newxmmc || !$newxmbh || !$newxmdh || !$newxmbh) {
            $this->error('不能为空', 'xmb/xmcopy');
        }

        $res = Model('xmb')->GetXmbDhAll($xmdh);
        if (!$res) {
            $this->error('不存在该项目代号', 'xmb/xmcopy');
        }
        //开始修改
        foreach ($res as $va) {
            $va['xmdh'] = $newxmdh;
            $va['xmbh'] = $newxmbh;
            $va['xmmc'] = $newxmmc;
        }
        unset($va['xid']);

        $data = Db::name('xt_xmb')->insertGetId($va);

        if (!$data) {
            $this->error('复制增加失败', 'xmb/xmcopy');
        }
        //增加日志
        Model('Log')->addLog("复制增加一个项目.$newxmmc", 1, $newxmdh);


        //这儿开始判断是否加项目名一样的检验目的
        if(isset($_POST['addJymd']) && $_POST['addJymd'] =='yes'){
                //开始增加检验目的
          } else
         {

            $this->success('复制添加成功', 'xmb/xmcopy');
         }

        //
    }

/*    /**
     * @return mixed
     * 外包项目增加页面
     */

    public function wbadd(){}
        /**
         * 先去查询下我的xmbh排序  获取当前最后的项目编号
         */
     /*   $data = Model('xmb')->GetWbXmBh();*/
/*
        return $this->fetch("xmb/main");
    }*/

    /***
     * 外包项目增加方法
     */
    public  function WbXmAdd(){
        $data = input('post.');
        /*Model('xmb')->();*/
        if(true){
            ///记录修改成功
            /*Model('Log')->addLog("修改项目允许上传.$xmmc",1,$xmmc);*/
            $this->success('修改成功','xmb/WbUpload');
        }
        $this->error('修改失败 检查项目名是否错误','WbUpload');

    }

    /**
     * 修改上传的pdf
     */
    public function WbUpload(){
        return $this->fetch("xmb/WbUpload");
    }
    /**
     * 修改上传的pdf
     */
    public function IsUploadWBReport(){
        $data = input('post.');
        if(empty($data['xmmc'])){
            $this->error("不能为空",'xmb/WbUpload');
        }
        $xmmc = rtrim($_POST['xmmc']);
        $res = Model('xmb')->IsUploadWBReport($xmmc);
        if($res){

            ///记录修改成功
            Model('Log')->addLog("修改项目允许上传*pdf*.$xmmc",1,$xmmc);
            $this->success('修改成功','xmb/WbUpload');
        }
        $this->error('修改失败 检查项目名是否错误','WbUpload');

    }


    /***
     *  修改不允许pdf
     */
    public function IsUploadWBReportNo(){
        $data = input('post.');
        if(empty($data['xmmc'])){
            $this->error("不能为空",'xmb/WbUpload');
        }
        $xmmc = rtrim($_POST['xmmc']);
        $res = Model('xmb')->IsUploadWBReportNo($xmmc);
        if($res){
            ///记录修改成功
            Model('Log')->addLog("修改项目不允许上传.$xmmc",1,$xmmc);
            $this->success('修改成功','xmb/WbUpload');
        }
        $this->error('修改失败 检查项目名是否错误','WbUpload');
    }

    /**
     * 项目查阅定位
     */
    public function consult(){
        return $this->fetch('xmb/consult');
    }
    /**
     *  根据项目名称来模糊搜索
     */
    public function  jymdsearch(){
        $srjymdmc = input('post.')['jymdmc'];
        $data = Db::name('xt_jymd')->
        where('jymdmc','like',"%".$srjymdmc."%") ->select();
        return $this->fetch('xmb/consultsearch');
    }


    /***
     * 一键加项目
     *
     */
    public function  onekeyxmcopy()
    {
        //$copyxmdh = Config('xmb.copyxmdh');
            $res = Model('xmb')->GetXmbDh("AC40%");
            foreach ($res as $k =>$va['xmdh']){
                //获取当前需要添加的最大项目代号
                $endXmdh = substr($va['xmdh']['xmdh'],2) + 5;
                $data['endXmdh'] = 'AC'.$endXmdh;
                //获取最大的项目编号
                //$res = Model('xmb')->GetXmbBhAll("YQ");
                //$endXmbh = substr($va['xmdh']['xmbh'],1) + 1;
                $data['endXmbh']  = 'Y'.Config('xmb.maxXmbh');
                setConfig('maxXmbh',Config('xmb.maxXmbh') + 1);
                break;
                //终止循环
            }
            //获取最大的检验目的
            $data['endJymddh'] = 'AC'.Config('xmb.maxJymd');
            setConfig('maxJymd',Config('xmb.maxJymd') + 1);
            /*$jymdRes = Model('jymd')->GetJymdDhDesc("7200%");
            foreach ($jymdRes as $k =>$va['jymddh']) {
                //获取最大的检验目的最大代号
                /*$endJymddh = substr($va['jymddh']['jymddh'],2) + 1;*/
                /*$endJymddh = $va['jymddh']['jymddh'] + 3;
                $data['endJymddh'] = 'AC'.$endJymddh;*/

              /*  break;
            }*/
            //获取外送委托单位
            $data['outhospital'] = Model('common')->GetOuthosiptal();

            //获取仪器代号
            $data['yqdh'] = Model('common')->GetYqdh();

            //获取标本种类
            $data['bbzl'] = Model('common')->GetBbzl();
            $this->assign('data',$data);
            return $this->fetch('xmb/onekeyxmcopy');
    }

    /***
     * 根据名字来添加 检验目的代码
     */
    public function  onekeyxmcopybyxmmc(){
        $copyxmdh = Config('xmb.copyxmdh');
        $hosiptalname = $_POST['hosiptalname'];
        $bbzl = $_POST['bbzl'];
        $bbzl = Model('common')->GetBbzlNumber($bbzl);
        $yqdh = $_POST['yqdh'];
        $newxmmc = $_POST['newxmmc'];
        $newxmdh = $_POST['newxmdh'];
        $newxmbh = $_POST['newxmbh'];
        $newjymddm = $_POST['newjymddm'];
        if (!$newxmmc || !$newxmbh || !$newxmdh || !$newxmbh) {
            $this->error('不能为空', 'xmb/onekeyxmcopy');
        }
        $res = Model('xmb')->GetXmbDhAll($copyxmdh);
        if (!$res) {
            $this->error('不存在该项目代号', 'xmb/onekeyxmcopy');
        }
        //开始修改
        foreach ($res as $va) {
            $va['xmdh'] = $newxmdh;
            $va['xmbh'] = $newxmbh;
            $va['xmmc'] = $newxmmc;
            $va['Outhosiptal'] = $hosiptalname;
            $va['yqdh'] = $yqdh;
            $va['pym']  = 'Ac';
            $va['ReportType'] ='PDF';
        }
        unset($va['xid']);
        //开始增加项目
        $data = Db::name('xt_xmb')->insertGetId($va);

        if (!$data) {
            $this->error('复制增加失败', 'xmb/onekeyxmcopy');
        }
        //增加日志
        Model('Log')->addLog("复制增加一个项目.$newxmmc", 1, $newxmdh);

        //检验目的增加 ---只限单项   iszh 0 和 1 区分
        $data = ['jymddh'=>$newjymddm, 'jymdmc'=>$newxmmc, 'pym'=>'Ac', 'bbzl'=>$bbzl, 'jyyq'=>','.$yqdh.',',
            'mdlb'=>'13', 'iszh'=>'0', 'zyz'=>'88', 'mdjc' =>$newxmmc];
        $xt_jymd_Res = Db::name('xt_jymd ')->insert($data);
        Model('Log')->addLog("检验目的代码:.$newjymddm.检验目的名称:.$newxmmc.外送.$hosiptalname.仪器平台.$yqdh.", 1, $newjymddm);

        //检验对照增加
        $data = ['jymddm'=>$newjymddm,'yqdh'=>$yqdh,'xmdm'=>$newxmdh,'custcode'=>'all'];
        $jcxmRes = Db::name('t_lis_jymd_jcxm ')->insert($data);

        if($xt_jymd_Res && $jcxmRes){
            $this->success("增加成功** 检验目的代号:$newjymddm.***名称:$newxmmc***外包单位:$hosiptalname.**标本种类：$bbzl.**.", 'xmb/onekeyxmcopy');
        }
        $this->success('增加失败', 'xmb/onekeyxmcopy');
    }

    /***
     * 一键加项目
     *
     */
    public function  onekeyxmadd(){
        return $this->fetch('xmb/onekeyxmcopy');
    }
}